//
//  DCOrderUploadViewController.m
//  JiuyaoMotorcade
//
//  Created by 大橙子 on 2018/11/29.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "DCOrderUploadViewController.h"

@interface DCOrderUploadViewController ()<TZImagePickerControllerDelegate>
@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) DCOrderReceiveOddModel *model;

@end

@implementation DCOrderUploadViewController

-(instancetype)initWithTransportModel:(DCOrderReceiveOddModel *)model {
    if (self = [super init]) {
        
        self.model = model;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"运单上传";
    self.view.backgroundColor = BackgroundColor;
    [self setUpUI];
}

- (void)setUpUI {
    CGFloat kBaseHeight = 40;
    CGFloat kImageViewHeight = 100*HEIGHTSCALE6;
    
    UIView *mainView = [[UIView alloc]initWithFrame:CGRectMake(10, 3, ScreenWidth - 20, 40*4+kImageViewHeight)];
    mainView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:mainView];
    
    UILabel *textLabel = [[UILabel alloc]init];
    textLabel.font = FONT(28);
    textLabel.textColor = BlueColor;
//    textLabel.text = [NSString stringWithFormat:@"%@   4S店",self.model.transportCode];
    textLabel.textAlignment = NSTextAlignmentCenter;
    [mainView addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(mainView);
        make.left.equalTo(mainView.mas_left);
        make.right.equalTo(mainView.mas_right);
        make.height.mas_offset(kBaseHeight);
    }];
    
    DCImageAndTextLabel *suvLabel = [[DCImageAndTextLabel alloc]init];
    suvLabel.imageView.image = IMAGENAME(@"SUV");
    //    suvLabel.textLabel.text = @"3台";
    suvLabel.textLabel.textColor = [UIColor blackColor];
    suvLabel.textLabel.attributedText = [YLAttributedStr changNumberColorWithTotalString:[NSString stringWithFormat:@"%@台",self.model.suvNumber] andColor:[UIColor redColor]];
    [mainView addSubview:suvLabel];
    [suvLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textLabel.mas_bottom).offset(3);
        make.left.equalTo(mainView.mas_left).offset(10);
        make.width.mas_offset(80*WIDTHSCALE6);
        make.height.mas_offset(kBaseHeight);
    }];
    
    DCImageAndTextLabel *carLabel = [[DCImageAndTextLabel alloc]init];
    carLabel.imageView.image = IMAGENAME(@"car");
    //    carLabel.textLabel.text = @"3台";
    carLabel.textLabel.textColor = [UIColor blackColor];
    carLabel.textLabel.attributedText = [YLAttributedStr changNumberColorWithTotalString:[NSString stringWithFormat:@"%@台",self.model.carNumber] andColor:[UIColor redColor]];
    [mainView addSubview:carLabel];
    [carLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textLabel.mas_bottom).offset(3);
        make.left.equalTo(suvLabel.mas_right).offset(10);
        make.width.mas_offset(80*WIDTHSCALE6);
        make.height.mas_offset(kBaseHeight);
    }];
    
    UILabel *totalLabel = [[UILabel alloc]init];
    totalLabel.font = FONT(16);
    totalLabel.textColor = [UIColor blackColor];
    totalLabel.textAlignment = NSTextAlignmentRight;
    totalLabel.attributedText = [YLAttributedStr changeSubStringWithTotalString:@"共计   6台" subStringColor:[UIColor redColor] subString:@"6" andFont:FONT(19)];
    totalLabel.attributedText =[YLAttributedStr changNumberColorWithTotalString:[NSString stringWithFormat:@"共计   %@台",self.model.totalNumber] andColor:[UIColor redColor]];
    [mainView addSubview:totalLabel];
    [totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(mainView.mas_right).offset(-10);
        make.left.equalTo(carLabel.mas_right).offset(10);
        make.top.equalTo(textLabel.mas_bottom).offset(3);
        make.height.mas_offset(kBaseHeight);
    }];
    
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image = IMAGENAME(@"上传照片");
    imageView.userInteractionEnabled = YES;
    self.imageView = imageView;
    [mainView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(suvLabel.mas_bottom).offset(15);
        make.centerX.equalTo(mainView);
        make.height.mas_offset(kImageViewHeight-10);
        make.width.mas_offset(kImageViewHeight-10);
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewClicked)];
    [imageView addGestureRecognizer:tap];
    
    
    UIButton * surtBtn = [[UIButton alloc]init];
    [surtBtn setTitle:@"提交" forState:UIControlStateNormal];
    surtBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [surtBtn addTarget:self action:@selector(surtBtnOnClicked) forControlEvents:UIControlEventTouchUpInside];
    surtBtn.layer.cornerRadius = 20;
    surtBtn.layer.masksToBounds = YES;
    surtBtn.backgroundColor = BlueColor;
    [mainView addSubview:surtBtn];
    [surtBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(kBaseHeight);
        make.left.equalTo(mainView.mas_left).offset(40);
        make.right.equalTo(mainView.mas_right).offset(-40);
        make.bottom.equalTo(mainView.mas_bottom).offset(-6);
    }];
}
-(void)imageViewClicked {
    __weak typeof(self) weakself = self;
    TZImagePickerController *imageVC = [[TZImagePickerController alloc]initWithMaxImagesCount:1 delegate:self];
    [imageVC setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        weakself.imageView.image = photos[0];
    }];
    [self presentViewController:imageVC animated:YES completion:nil];
}
-(void)surtBtnOnClicked {
    DCLog(@"上传订单");
    
//    __weak typeof(self) weakself = self;
//    [DCServiceTool uploadSingleImage:self.imageView.image success:^(id responseObject) {
//        DCLog(@"re---%@",responseObject);
//        [weakself transportUploadWithFileName:responseObject[@"fileName"] filePath:responseObject[@"filePath"]];
//    } failure:^(NSError *error) {
//        
//    }];
}

-(void)transportUploadWithFileName:(NSString *)fileName filePath:(NSString *)filePath {
    
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
//    info[@"transportCode"] = self.model.transportCode;
    info[@"fileName"] = fileName;
    info[@"filePath"] = filePath;
    DCLog(@"info----%@",info);
    __weak typeof(self) weakself = self;
//    [DCServiceTool postWithUrl:transportUpload_URL params:info success:^(id responseObject) {
//        if ([responseObject[@"code"] integerValue] == 0) {
//            [NOTIFICATION postNotificationName:@"transportUploadSuccess" object:nil];
//            [weakself showMessage:@"上传成功" afterDelay:1.0 completion:^{
//                [weakself.navigationController popViewControllerAnimated:YES];
//            }];
//        }else {
//            [weakself showMessage:responseObject[@"msg"] afterDelay:1.0];
//        }
//    } failure:^(NSError *error) {
//        
//    }];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
